package cn.sfrog.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.sfrog.common.constant.Constants;
import cn.sfrog.common.utils.SecurityUtils;
import cn.sfrog.common.utils.StringUtils;
import cn.sfrog.system.domain.SysNotice;
import cn.sfrog.system.domain.SysNoticeUserReadRecord;
import cn.sfrog.system.domain.vo.app.AppNoticeResultVO;
import cn.sfrog.system.mapper.SysNoticeMapper;
import cn.sfrog.system.mapper.SysNoticeUserReadRecordMapper;
import cn.sfrog.system.service.ISysNoticeService;
import cn.sfrog.system.service.ISysNoticeUserReadRecordService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 公告 服务层实现
 */
@Service
public class SysNoticeServiceImpl implements ISysNoticeService {
    @Autowired
    private SysNoticeMapper noticeMapper;
    @Resource
    private SysNoticeUserReadRecordMapper sysNoticeUserReadRecordMapper;
    @Autowired
    private ISysNoticeUserReadRecordService sysNoticeUserReadRecordService;
    /**
     * 查询公告信息
     *
     * @param noticeId 公告ID
     * @return 公告信息
     */
    @Override
    public SysNotice selectNoticeById(Long noticeId) {
        return noticeMapper.selectNoticeById(noticeId);
    }

    /**
     * 查询公告列表
     *
     * @param notice 公告信息
     * @return 公告集合
     */
    @Override
    public List<SysNotice> selectNoticeList(SysNotice notice) {
        return noticeMapper.selectNoticeList(notice);
    }

    /**
     * 新增公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int insertNotice(SysNotice notice) {
        return noticeMapper.insertNotice(notice);
    }

    /**
     * 修改公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int updateNotice(SysNotice notice) {
        return noticeMapper.updateNotice(notice);
    }

    /**
     * 删除公告对象
     *
     * @param noticeId 公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeById(Long noticeId) {
        return noticeMapper.deleteNoticeById(noticeId);
    }

    /**
     * 批量删除公告信息
     *
     * @param noticeIds 需要删除的公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeByIds(Long[] noticeIds) {
        return noticeMapper.deleteNoticeByIds(noticeIds);
    }

    @Override
    public List<AppNoticeResultVO> selectAppNoticeList(Long userId) {
        List<AppNoticeResultVO> noticeList = noticeMapper.selectAppNoticeList(userId);
        noticeList.forEach(notice ->{
            LambdaQueryWrapper<SysNoticeUserReadRecord> queryWrapper = new LambdaQueryWrapper<SysNoticeUserReadRecord>()
                    .eq(SysNoticeUserReadRecord::getNoticeId,notice.getNoticeId())
                    .eq(SysNoticeUserReadRecord::getUserId,userId);

            SysNoticeUserReadRecord readRecord = sysNoticeUserReadRecordMapper.selectOne(queryWrapper);
            if(StringUtils.isNull(readRecord)){
                //未读
                notice.setIsRead(false);
            }else {
                //已读
                notice.setIsRead(true);
            }
        });
        return noticeList;
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public SysNotice selectAppNoticeDetail(Long noticeId) {
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        SysNotice sysNotice = noticeMapper.selectNoticeById(noticeId);
        //设置通知消息已读
        LambdaQueryWrapper<SysNoticeUserReadRecord> queryWrapper = new LambdaQueryWrapper<SysNoticeUserReadRecord>()
                .eq(SysNoticeUserReadRecord::getNoticeId,noticeId)
                .eq(SysNoticeUserReadRecord::getUserId,userId);
        SysNoticeUserReadRecord readRecord = sysNoticeUserReadRecordMapper.selectOne(queryWrapper);
        if(StringUtils.isNull(readRecord)){
            SysNoticeUserReadRecord record = new SysNoticeUserReadRecord();
            record.setUserId(userId);
            record.setNoticeId(noticeId);
            record.insert();
        }
        return sysNotice;
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public Boolean allRead() {
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        LambdaQueryWrapper<SysNoticeUserReadRecord> queryWrapper = new LambdaQueryWrapper<SysNoticeUserReadRecord>()
                .eq(SysNoticeUserReadRecord::getUserId,userId);
        List<SysNoticeUserReadRecord> readRecordList = sysNoticeUserReadRecordMapper.selectList(queryWrapper);
        List<Long> readNoticeIds = readRecordList.stream().map(SysNoticeUserReadRecord::getNoticeId).collect(Collectors.toList());

        //所有的系统公告
        LambdaQueryWrapper<SysNotice> lambdaQueryWrapper = new LambdaQueryWrapper<SysNotice>()
                .eq(SysNotice::getStatus, Constants.STATUS_NORMAL)
                .notIn(!readNoticeIds.isEmpty(),SysNotice::getNoticeId,readNoticeIds);
        List<SysNotice> sysNotices = SysNotice.DAO.selectList(lambdaQueryWrapper);
        List<SysNoticeUserReadRecord> saveList = new ArrayList<>();
        for (SysNotice notice:sysNotices) {
            SysNoticeUserReadRecord record = new SysNoticeUserReadRecord();
            record.setUserId(userId);
            record.setNoticeId(notice.getNoticeId());
            saveList.add(record);
        }
        if(CollectionUtil.isNotEmpty(saveList)){
            sysNoticeUserReadRecordService.saveBatch(saveList, saveList.size());
        }
        return true;
    }
}
